package cn.ffcs.uec.outStaff.component;

import java.util.HashMap;
import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.IdSpace;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zkplus.databind.BindingListModelList;
import org.zkoss.zul.Div;
import org.zkoss.zul.ListModel;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

import cn.ffcs.raptornuke.plugin.common.zk.util.ZkUtil;
import cn.ffcs.uec.common.util.PermissionUtil;
import cn.ffcs.uec.outStaff.component.bean.UecOutStaffHiPsnWorkListboxBean;
import cn.ffcs.uec.outStaff.constant.UecOutStaffConstants;
import cn.ffcs.uec.outStaff.model.UecOutStaffInfo;
import cn.ffcs.uec.staff.manager.UecWorkManager;
import cn.ffcs.uec.staff.model.UecWork;
import cn.ffcs.uom.common.constants.BaseUnitConstants;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.vo.PageInfo;
import cn.ffcs.uom.common.zul.PubUtil;
import lombok.Getter;
import lombok.Setter;

/**
 * 外包人员履历记录
 * 
 * @Title:
 * @Description:
 * @author Wang Yiren
 * @版权 FFCS（C） 2017
 * @date 2018年9月27日
 */
@Controller
@Scope("prototype")
public class UecOutStaffHiPsnWorkListboxExt extends Div implements IdSpace {

    private static final long serialVersionUID = -8997743822660809291L;

    private static final String UEC_OUTSATFF_HI_PSNWORK_EXT_LISTBOX_ZUL = "/pages_nmg/uec_outStaff/comp/uec_outStaff_HiPsnWork_listbox_ext.zul";

    private static final String UEC_OUTSATFF_HI_PSNWORK_EDIT_ZUL = "/pages_nmg/uec_outStaff/comp/uec_outStaff_HiPsnWork_edit.zul";

    private UecOutStaffHiPsnWorkListboxBean bean = new UecOutStaffHiPsnWorkListboxBean();

    /**
     * 外包人员履历记录
     */
    private UecWork uecWork;

    private UecOutStaffInfo uecOutStaffInfo;

    private UecWorkManager uecWorkManager = (UecWorkManager) ApplicationContextUtil.getBean("uecWorkManager");

    @Getter
    @Setter
    private IPortletInfoProvider portletInfoProvider;

    public UecOutStaffHiPsnWorkListboxExt() {
        Executions.createComponents(UEC_OUTSATFF_HI_PSNWORK_EXT_LISTBOX_ZUL, this, null);
        Components.wireVariables(this, bean);
        Components.addForwards(this, this, '$');
        this.addForward(UecOutStaffConstants.ON_UEC_OUTSTAFF_HI_PSNWORK_QUERY, this,
                UecOutStaffConstants.ON_UEC_OUTSATFF_HI_PSNWORK_QUERY_RESPONSE);
        this.addForward(UecOutStaffConstants.ON_CLEAN_OUTSTAFF_HI_PSNWORK, this,
                UecOutStaffConstants.ON_CLEAN_OUTSATFF_HI_PSNWORK_QUERY_RESPONSE);
    }

    /**
     * 初始化
     */
    public void onCreate() {
        this.setButtonValid(false, false, false, false);
    }

    /**
     * 选择查询记录
     * 
     * @throws Exception
     */
    public void onSelectUecOutStaffHiPsnWorkList() throws Exception {
        if (this.bean.getUecOutStaffHiPsnWorkListbox().getSelectedCount() > 0) {
            uecWork = (UecWork) bean.getUecOutStaffHiPsnWorkListbox().getSelectedItem().getValue();
        }
    }

    /**
     * 人员基本信息功能，抛出事件， 事件监听
     * 
     * @param event
     */
    public void onUecOutStaffHiPsnWorkQueryResponse(ForwardEvent event) {
        uecOutStaffInfo = (UecOutStaffInfo) event.getOrigin().getData();
        if (null != uecOutStaffInfo) {
            //设置操作按钮，非盟市主管不可操作按钮
            if(PermissionUtil.isNoMainOutStaff()) {
                this.setButtonValid(false, true, true, true);
            }else {
                this.setButtonValid(true, true, true, true);
            }
            uecWork = new UecWork();
            uecWork.setStaffId(uecOutStaffInfo.getOutStaffId());
            uecWork.setPkPsndoc(uecOutStaffInfo.getPkPsnMsg());
            uecWork.setNmStaffType(BaseUnitConstants.NM_OUT_STAFF_TYPE);           
            this.onQueryUecOutStaffHiPsnWork();
        }
    }

    public void onCleanUecOutStaffHiPsnWorkResponse(ForwardEvent event) {
        clearVariable();
        uecOutStaffInfo = null;
        ListboxUtils.clearListbox(this.bean.getUecOutStaffHiPsnWorkListbox());
        this.bean.getUecOutStaffHiPsnWorkListboxPaging().setTotalSize(1);
    }

    /**
     * 新增按钮
     * 
     * @throws Exception @throws
     */
    public void onAddUecOutStaffHiPsnWork() throws Exception {
        if (uecOutStaffInfo != null) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("opType", "add");
            map.put("uecOutStaffInfo", uecOutStaffInfo);
            Window window = (Window) Executions.createComponents(UEC_OUTSATFF_HI_PSNWORK_EDIT_ZUL, this, map);
            window.doModal();
            window.addEventListener("onOK", new EventListener() {

                @Override
                public void onEvent(Event event) throws Exception {
                    if (event.getData() != null) {
                        // 新增或修改完成后，列表从数据库更新
                        uecWork = (UecWork) event.getData();
                        onQueryUecOutStaffHiPsnWorkList();
                    }
                }
            });
        }
    }

    /**
     * 编辑按钮
     * 
     * @throws Exception
     * @throws InterruptedException
     */
    public void onEditUecOutStaffHiPsnWork() throws Exception, InterruptedException {
        if (uecWork != null && null != uecWork.getId()) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("opType", "edit");
            map.put("uecWork", uecWork);
            Window window = (Window) Executions.createComponents(UEC_OUTSATFF_HI_PSNWORK_EDIT_ZUL, this, map);
            window.doModal();
            window.addEventListener("onOK", new EventListener() {

                @Override
                public void onEvent(Event event) throws Exception {
                    if (event.getData() != null) {
                        // 新增或修改完成后，列表从数据库更新
                        uecWork = (UecWork) event.getData();
                        onQueryUecOutStaffHiPsnWorkList();
                    }
                }
            });
        } else {
            ZkUtil.showError("请选择要编辑的记录", "提示信息");
            return;
        }
    }
    
    /**
     * 删除按钮
     * @Title: onDelUecOutStaffHiPsnWork   
     * @Description: TODO 
     * @param:       
     * @return: void      
     * @throws
     */
	public void onDelUecOutStaffHiPsnWork() {
		if (uecWork != null) {
			ZkUtil.showQuestion("确定要删除吗?", "提示信息", new EventListener() {
				public void onEvent(Event event) throws Exception {
					Integer result = (Integer) event.getData();
					if (result == Messagebox.OK) {
						uecWork.removeOnly();
						PubUtil.reDisplayListbox(bean.getUecOutStaffHiPsnWorkListbox(),
								uecWork, "del");
						uecWork = null;
					}
				}
			});
		} else {
			ZkUtil.showError("请选择你要删除的记录", "提示信息");
			return;
		}
	}

    /**
     * 查询按钮
     */
    public void onQueryUecOutStaffHiPsnWork() {
        this.bean.getUecOutStaffHiPsnWorkListboxPaging().setActivePage(0);
        this.onQueryUecOutStaffHiPsnWorkList();
    }

    /**
     * 设置按钮
     * 
     * @param canAdd
     * @param candel
     */
    private void setButtonValid(boolean canView, boolean canAdd, boolean canEdit, boolean candel) {
        this.bean.getViewButton().setDisabled(!canView);
        this.bean.getAddButton().setDisabled(!canAdd);
        this.bean.getEditButton().setDisabled(!canEdit);
        this.bean.getDelButton().setDisabled(!candel);
    }

    /**
     * 清除变量
     */
    public void clearVariable() {
        uecWork = null;
    }

    /**
     * 翻页
     */
    public void onUecOutStaffHiPsnWorkListboxPaging() {
        this.onQueryUecOutStaffHiPsnWorkList();
    }

    /**
     * 查询
     */
    public void onQueryUecOutStaffHiPsnWorkList() {
        if (null != uecWork) {
            PageInfo pageInfo = uecWorkManager.queryPageInfoByUecWork(uecWork,
                    this.bean.getUecOutStaffHiPsnWorkListboxPaging().getActivePage() + 1,
                    this.bean.getUecOutStaffHiPsnWorkListboxPaging().getPageSize());
            ListModel list = new BindingListModelList(pageInfo.getDataList(), true);
            this.bean.getUecOutStaffHiPsnWorkListbox().setModel(list);
            this.bean.getUecOutStaffHiPsnWorkListboxPaging().setTotalSize(pageInfo.getTotalCount());
        }
    }
}
